<?php

	namespace Phptricks\app\controllers\countrys;

/*
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 */
	/**
	 * Description of newPHPClass
	 *
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\app\controllers\base\base as base;
	use Phptricks\app\models\countrys\countrys as countrys_model;

	class countrys extends base{

		public $countrys_model = null;

		public function __construct(){
			parent::__construct();
			$this->countrys_model = countrys_model::getinstance();
		}

		public function _initialize(){
			
		}

		public function admin_index(){
			$data = $this->countrys_model->getcountrys();
			$this->view->assign('data', $data);
			$this->view->display('countrys.tpl');
		}

		public function admin_view(){
			$this->admin_index();
		}

		public function admin_add(){
			$this->view->display('countrys.tpl');
		}

		public function admin_addnew(){
			$this->form_validation->post(array(
				'country_name' => 'isnull',
				'country_home' => 'isnull|digit'
			));

			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), 'admin/countrys/add/', 5);
			}
			$this->countrys_model->addcountrys($this->form_validation->_array_data);
			$this->display_message('success', $this->lang['success_add_data'], 'admin/countrys/view/', 3);
		}

		public function admin_edit($id = 0){
			if (empty($id)) {
				$this->display_message('danger', $this->lang['error_select_data'], 'admin/countrys/view/', 5);
			}
			$id = \intval($id);
			$where_array = array(
				'country_id' => $id
			);
			$data = $this->countrys_model->getcountrys($where_array);
			$this->view->assign('data', $data[0]);
			$this->view->display('countrys.tpl');
		}

		public function admin_update(){
			$this->form_validation->post(array(
				'country_id' => 'isnull|digit',
				'country_name' => 'isnull',
				'country_home' => 'isnull|digit'
			));

			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$url = 'admin/countrys/edit/' . $this->form_validation->_array_data['country_id'];
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), $url, 5);
			}
			$id = $this->form_validation->_array_data['country_id'];
			$array_where = array(
				'country_id' => $id
			);
			$this->form_validation->unset_var('country_id', 'post');
			$this->countrys_model->updatecountrys($this->form_validation->_array_data, $array_where);
			$this->display_message('success', $this->lang['success_edit_data'], 'admin/countrys/view/', 3);
		}

		public function admin_delete($id){
			if (!empty($id) && isset($id)) {
				if (\is_array($id)) {
					$id = \array_filter($id, function(&$id_val) {
						echo $id_val . "<br>";
						return \intval($id_val);
					});
				} else {
					$id = \intval($id);
				}
				$where_array = array(
					'country_id' => $id
				);
				$this->countrys_model->deletecountrys($where_array);
				$this->display_message('success', $this->lang['success_delete_data'], 'admin/countrys/view/', 3);
			}
		}

		public function admin_action(){
			if ($this->form_validation->is_set('row_id') == false) {
				$this->display_message('danger', $this->lang['error_select_data'], 'admin/countrys/view/', 5);
			}
			$id = $this->form_validation->_post['row_id'];
			if (\is_array($id)) {
				$id = \array_filter($id, function(&$id_val) {
					return \intval($id_val);
				});
			} else {
				$id = \intval($id);
			}

			if (!empty($id)) {
				$where_array = array(
					'country_id' => $id
				);
				try {
					$this->countrys_model->deletecountrys($where_array);
				} catch (\Exception $exc) {
					throw new Exc($exc->getMessage());
				}
				$this->display_message('success', $this->lang['success_delete_data'], 'admin/countrys/view/', 3);
			}
		}

	}
	